#!/usr/bin/sh
#Version 190125.1

echo "Fortis FS9000/9200 Enigma2"

modprobe e2_proc

echo "[rcS] Init frontpanel"
if [ ! -e /dev/vfd ]; then
   mknod -m 0660 /dev/vfd c 147 0
fi
modprobe nuvoton

if [ -e /var/etc/.firstboot ]; then
   echo "Wait 1 min." > /dev/vfd
   echo "[rcS] WAIT 1 minute"
   rm /var/etc/.firstboot
   sleep 1
   touch /dev/.devfsd
else
   echo "LOAD 10" > /dev/vfd
   echo "[rcS] LOAD 10"
fi

if [ -e /etc/init.d/mountvirtfs ]; then
   /etc/init.d/mountvirtfs start
fi

echo "[rcS] Starting DEBUG Interface"
if [ -e /etc/init.d/mountall ]; then
   /etc/init.d/mountall start
fi

echo "[rcS] Init stmfb (LOAD 9)"
echo "LOAD 9" > /dev/vfd
modprobe stmcore-display-stx7109c3
#if grep -q "config.av.videoport=Scart" /etc/enigma2/settings; then
#  modprobe stmfb display0=720x576-32@50:15m:pal:rgb:rgb #SCART
#else
  modprobe stmfb display0=1280x720-32@50:16m:pal:rgb:rgb; #HDMI
#fi

echo "[rcS] Loading modules"

echo "[rcS] Load audio firmware (LOAD 8)"
echo "LOAD 8" > /dev/vfd
/usr/bin/ustslave /dev/st231-1 /boot/audio.elf > /dev/null

echo "[rcS] Load video firmware (LOAD 7)"
echo "LOAD 7" > /dev/vfd
/usr/bin/ustslave /dev/st231-0 /boot/video.elf > /dev/null

echo "[rcS] Init embx (LOAD 6)"
echo "LOAD 6" > /dev/vfd
modprobe embxshell
modprobe embxmailbox mailbox0=0xb9211000:136:set2 mailbox1=0xb9212000:0
modprobe embxshm mailbox0=shm:0:7:0x60000000:0:256:256:0:1024:0xa4000000:0x10000000
modprobe mme_host transport0=shm

echo "[rcS] Init AVS (LOAD 5)"
echo "LOAD 5" > /dev/vfd
modprobe avs type=stv6412

player=`cat /proc/stb/player/version | awk '{ print substr( $0, length($0) - 2, length($0) ) }'`
echo "[rcS] Init player $player (LOAD 4)"
echo "LOAD 4" > /dev/vfd
modprobe mmelog
modprobe mpeg2hw mmeName=MPEG2_TRANSFORMER1;
modprobe stmsysfs
modprobe ksound
modprobe p2div64
modprobe stm_v4l2
modprobe pseudocard

echo "[rcS] Init frontends"
modprobe stv090x
echo "[rcS] Init CI & cardreader"
modprobe starci

modprobe pti waitMS=20 videoMem=4096
if [ $? -ne 0 ]; then
   modprobe pti
   echo "[rcS] Simple pti"
else
   echo "[rcS] Full feature pti"
fi
modprobe stm_monitor
modprobe stmdvb

echo "[rcS] Init player2 (LOAD 3)"
echo "LOAD 3" > /dev/vfd
modprobe player2 noaudiosync=1; discardlateframe=0; useoldaudiofw=1;
modprobe sth264pp
modprobe stmalloc
modprobe platform

if [ -e /usr/share/bootlogo.mvi ]; then
   showiframe -p /usr/share/bootlogo.mvi &
fi

if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/exportfs ]; then
   modprobe exportfs
fi
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/cifs ]; then
   modprobe cifs
fi
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/ntfs ]; then
   modprobe ntfs
fi
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/tun ]; then
   modprobe tun
fi
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/usbserial ]; then
   modprobe usbserial
fi
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/ftdi ]; then
   modprobe ftdi
fi
if [ -e /lib/modules/2.6.32.71_stm24_0217/extra/pl2303 ]; then
   modprobe pl2303
fi

echo "[rcS] Init frontpanel (LOAD 2)"
echo "LOAD 2" > /dev/vfd
modprobe boxtype
modprobe simu_button

echo "[rcS] Init remote control"
echo "Load 2" > /dev/vfd
mknod /dev/rc c 147 1
/usr/bin/evremote2 10 140 &

echo "[rcS] Init smartcard cardreader"
mknod /dev/sci0 c 253 0
mknod /dev/sci1 c 253 1
#modprobe sci-ng

echo -n "[rcS] Init WLAN: "
wlan=`cat /etc/enigma2/settings | grep systemoptions.wlan | cut -d "=" -f2`
if [ ! $wlan ] || [ ! -e /lib/modules/2.6.32.71_stm24_0217/extra/$wlan ]; then
   echo "None."
else
   modprobe $wlan
   echo "[rcS] $wlan WLAN"
#   echo "$wlan WLAN" > /dev/vfd
#   sleep 3
fi

# Start automount
if [ -e /etc/init.d/automount ]; then
   python /etc/init.d/automount &
fi

# Enable PKT plugins
if [ ! -e /tmp/reference.dat ] && [ -e /var/usr/bin/reference.dat ]; then
   cp /var/usr/bin/reference.dat /tmp
fi

echo "[rcS] Init portmap & ftp (LOAD 1)"
echo "LOAD 1" > /dev/vfd
if [ -e /etc/init.d/portmap ]; then
   /etc/init.d/portmap start
fi

if [ -e /etc/init.d/vsftpd ]; then
   /etc/init.d/vsftpd start
fi
/usr/sbin/inetd

# Do own extras
if [ -e /etc/ownscript.sh ]; then
   /etc/ownscript.sh &
fi

#stfbcontrol a 255

echo "Loading E2" > /dev/vfd
echo "[rcS] Loading E2"

until false
do
   echo "[rcS] Entering e2 main loop"
   if [ -e /usr/share/backdrop.mvi ]; then
      showiframe -p /usr/share/backdrop.mvi &
   fi
   /usr/bin/enigma2
   rtv=$?
   echo "[rcS] e2 ended <- return value: " $rtv
   /usr/bin/fp_control -i 40 0
   case "$rtv" in
      0) echo "E2 ERROR"
         echo "E2 Error" > /dev/vfd
         init 6;;
      1) echo "SHUTDOWN"
         echo "Shutdown" > /dev/vfd
         if [ -e /usr/share/shutdown.mvi ]; then
            showiframe -p /usr/share/shutdown.mvi &
         fi
         init 0;;
      2) echo "REBOOT"
         echo "Reboot" > /dev/vfd
         init 6;;
      3) echo "E2 RESTART"
         echo "Restart GUI" > /dev/vfd
         echo "0" > /proc/progress
         if [ -e /usr/share/backdrop.mvi ]; then
            showiframe -p /usr/share/backdrop.mvi &
         else
            showiframe -p /usr/share/bootlogo.mvi &
         fi;;
      *) echo "ERROR"
         echo "Error" > /dev/vfd
         init 6;;
   esac
done
